package com.arm.service.impl;

import com.arm.module.User;
import com.arm.module.dto.Regedit;
import com.arm.service.BaseService;
import com.arm.service.RegeditService;
import com.arm.service.UserService;
import com.arm.tools.AliSMSTools;
import com.arm.tools.GenerateID;
import com.arm.tools.myRunTime.MyRunTimeExcption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Baggio on 2020-5-22.
 */
@Service
public class RegeditServiceImpl extends BaseService implements RegeditService {
    @Autowired
    UserService userService;

    @Override
    public User regedit(Regedit regedit) {
        String phone = regedit.getPhone();
        User user = userService.getUserByPhone(phone);
        if (user == null) {
            user = new User();
            user.setPhone(phone);
            user.setUserName(regedit.getUserName());
            user.setCreateTime(new Date());
            user.setUnit("无");
            user.setStatus(User.STATUS_VALID);
        }
        user.setOpenId(regedit.getOpenId());
        user.setHeadPic(regedit.getPhoto());
        userService.modifyUser(user);

        return user;
    }

    @Override
    public String getCode(String phone) throws MyRunTimeExcption {
        String code = GenerateID.getRandomNum(6, GenerateID.IS_ONLY_NUMBER);
        Map<String, String> map = new HashMap<>();
        map.put("code", code);
        AliSMSTools.sendSMS(map, phone, AliSMSTools.SMS_TEMPLATE_REGEDIT);
        return code;
    }
}
